﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using HCFramework.Product.FilterManage;

namespace HCFramework.Product.ProductManage
{
    public partial class ChoosePropertyAndFilter : System.Web.UI.UserControl
    {
        public int? ProductID
        {
            get
            {
                if (litProductID.Text == "") return null;
                return int.Parse(litProductID.Text);
            }
            set
            {
                if (value.HasValue)
                    litProductID.Text = value.Value.ToString();
                else
                    litProductID.Text = "";
            }
        }

        public int? CopyProductID
        {
            get
            {
                if (litCopyProductID.Text == "") return null;
                return int.Parse(litCopyProductID.Text);
            }
            set
            {
                if (value.HasValue)
                    litCopyProductID.Text = value.Value.ToString();
                else
                    litCopyProductID.Text = "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            BindDefaultData();
            if (!IsPostBack)
            {
                if (ProductID.HasValue) BindForm(ProductID.Value);
                if (CopyProductID.HasValue) BindForm(CopyProductID.Value);
            }
        }

        private void BindDefaultData()
        {
            List<ProductProperty> listProductProperty = C.ProductCategoryController.GetByID(M.UrlQuery.categoryID.Value).ProductProperties.ToList();
            foreach (ProductProperty property in listProductProperty)
            {
                TableRow row = new TableRow();
                TableCell titleCell = new TableCell();
                TableCell inputPropertyCell = new TableCell();
                TableCell inputFilterCell = new TableCell();

                row.Cells.Add(titleCell);
                row.Cells.Add(inputPropertyCell);
                row.Cells.Add(inputFilterCell);
                Table1.Rows.Add(row);

                titleCell.Text = property.Name;
                TextBox txtProductProperty = new TextBox();
                txtProductProperty.Attributes["ProductPropertyID"] = property.ProductPropertyID.ToString();
                inputPropertyCell.Controls.Add(txtProductProperty);
                ChooseFilter chooseFilter = (ChooseFilter)LoadControl(@"~\Product\Admin\FilterManage\ChooseFilter.ascx");
                chooseFilter.BindData(property.ProductPropertyID);
                inputFilterCell.Controls.Add(chooseFilter);
            }
        }

        internal void BindForm(int productId)
        {
            foreach (TableRow row in Table1.Rows)
            {
                TextBox txtProductProperty = row.Cells[1].Controls[0] as TextBox;
                ChooseFilter chooseFilter = row.Cells[2].Controls[0] as ChooseFilter;

                int propertyID = int.Parse(txtProductProperty.Attributes["ProductPropertyID"]);
                ProductPropertyValue ppv = C.ProductPropertyValueController.GetOrInsert(productId, propertyID, new HCDataContext());

                txtProductProperty.Text = ppv.Value;
                chooseFilter.SelectedIDs = C.Product_PropertyFilterController.GetList(productId, propertyID).Select(ik => ik.PropertyFilterID).ToList();
            }
        }

        internal void Save(int productId)
        {
            HCDataContext db = new HCDataContext();
            foreach (TableRow row in Table1.Rows)
            {
                TextBox txtProductProperty = row.Cells[1].Controls[0] as TextBox;
                ChooseFilter chooseFilter = row.Cells[2].Controls[0] as ChooseFilter;

                int propertyID = int.Parse(txtProductProperty.Attributes["ProductPropertyID"]);
                ProductPropertyValue ppv = C.ProductPropertyValueController.GetOrInsert(productId, propertyID, db);
                ppv.Value = txtProductProperty.Text;
                C.ProductPropertyValueController.Update(ppv, db);

                C.Product_PropertyFilterController.Clear(productId, propertyID);
                C.Product_PropertyFilterController.Inserts(productId, chooseFilter.SelectedIDs);
            }
        }
    }
}